﻿using APP.QUANLY.Common;
using MetroUI.Forms;
using RMAS.DAL;
using Sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMAS
{
    public partial class frmRegisterSoftWare : MetroForm
    {
        DM_BRANCH_Provider _prov = new DM_BRANCH_Provider();
        RegistrySetting setting = new RegistrySetting();
        SoftLicence SL = new SoftLicence();
        RunTypes runtypes = RunTypes.UnKnown;
        public bool _IsAcitive = false;
        public bool IsActive { get { return _IsAcitive; } set { _IsAcitive = value;} }

        public frmRegisterSoftWare(SoftLicence MySL, RunTypes runtypes)
        {
            InitializeComponent();
            this.SL = MySL;
            this.runtypes = runtypes; 
        }

        private void frmRegisterSoftWare_Load(object sender, EventArgs e)
        {
            if (this.runtypes == RunTypes.Trial || this.runtypes == RunTypes.Expired)
            {
                SetTextDefault();
                if (SL.SerialKey != "")
                {
                    txtKey1.Text = SL.SerialKey.Substring(0, 4).ToString();
                    txtKey2.Text = SL.SerialKey.Substring(4, 4).ToString();
                    txtKey3.Text = SL.SerialKey.Substring(8, 4).ToString();
                    txtKey4.Text = SL.SerialKey.Substring(12, 4).ToString();
                }
                if (this.runtypes == RunTypes.Trial)
                {
                    if ((SL.Count > 0) && (SL.Count <= 30))
                    {
                        btnDungThu.Text = "Dùng Thử (" + SL.Count + " ngày)";
                    }
                }
                else
                {
                    btnDungThu.Text = "Thoát";
                    MessageBox.Show("Bạn đã hết hạn dùng thử!\nVui lòng liên hệ nhà cung cấp theo thông tin sau để được cấp LicenceKey!!!");
                    frmHeplerInfo info = new frmHeplerInfo();
                    info.ShowDialog();
                   
                    return;
                }
            }
        }
        private void SetTextDefault()
        {
            labMa.Text = "";
            txtLiKey1.Text = "";
            txtLiKey2.Text = "";
            txtLiKey3.Text = "";
            txtLiKey4.Text = "";

            txtLiKey1.Select();
            txtLiKey1.Focus();
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtLiKey1.Name)
                    UIHelper.txt_KeyPress(sender, txtLiKey2, e);
                if (txt.Name == txtLiKey2.Name)
                    UIHelper.txt_KeyPress(sender, txtLiKey3, e);
                if (txt.Name == txtLiKey3.Name)
                    UIHelper.txt_KeyPress(sender, txtLiKey4, e);
                if (txt.Name == txtLiKey4.Name)
                    UIHelper.txt_KeyPress(sender, btnDangKy, e);
            }
            catch { }
        }
        private void btnDungThu_Click(object sender, EventArgs e)
        {
            if (btnDungThu.Text == "Thoát")
            {
                Environment.Exit(0);
            }
            this.DialogResult = DialogResult.OK;

        }
        private void btnDangKy_Click(object sender, EventArgs e)
        {
            SL.LicenceKey = txtLiKey1.Text + txtLiKey2.Text + txtLiKey3.Text + txtLiKey4.Text;
            bool KQ = setting.CheckActive(SL);
            if (KQ)
            {
                MessageBox.Show("Xác thực thành công!");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Xác thực không thành công! \r\n Vui lòng liên hệ nhà cung cấp để được cấp key.");
                SetTextDefault();
                return;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case (Keys.F4 | Keys.Alt):
                    Environment.Exit(0);
                    return true;
                case (Keys.Escape):
                    Environment.Exit(0);
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }  
    }
}
